package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.service.AppletUserService;
import com.system.runtai.service.IpaCollectService;
import com.system.runtai.service.IpaEnterService;
import com.system.runtai.service.IpaService;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.PrimitiveIterator;

/**
 * <p>
 * 通讯录收藏 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "通讯录收藏")
@RestController
@RequestMapping("/runtai/ipa-collect")
public class IpaCollectController {


    @Autowired
    private IpaEnterService ipaEnterService;

    @Autowired
    private IpaCollectService ipaCollectService;

    @Autowired
    private IpaService ipaService;

    @Autowired
    private AppletUserService appletUserService;

    @ApiOperation(value = "程序的通讯录收藏列表")
    @GetMapping("/selectList")
    public Result<IPage<IpaEnter>> selectList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit,IpaEnter car){
        String s = AuthContextUtil.get();
        LambdaQueryWrapper<IpaCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IpaCollect::getAppletUserId,Integer.valueOf(s));
        List<IpaCollect> list = ipaCollectService.list(wrapper);

        List<IpaEnter> carList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Integer carId = list.get(i).getIpaId();
            IpaEnter byId = ipaEnterService.getById(carId);
            carList.add(byId);
        }

if (carList.size()>1) {
    for (IpaEnter ipa : carList) {
        LambdaQueryWrapper<IpaEnter> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.orderByDesc(IpaEnter::getCreateTime).eq(IpaEnter::getId, ipa.getId());
//            List<IpaEnter> list1 = ipaService.list(wrapper1);
        Page<IpaEnter> recommendPage = new Page<>(page, limit);
        IPage<IpaEnter> iPage = ipaEnterService.page(recommendPage, wrapper1);


        for (IpaEnter record : iPage.getRecords()) {

            LambdaQueryWrapper<IpaCollect> wrapper2 = new LambdaQueryWrapper<>();
            wrapper2.eq(IpaCollect::getAppletUserId,Integer.valueOf(s)).eq(IpaCollect::getIpaId,record.getId());
            IpaCollect one1 = ipaCollectService.getOne(wrapper2);

            if (one1!=null){
                record.setCollect(1);
            }else {
                record.setCollect(0);
            }
        }

        return Result.ok(iPage);
    }
}

        LambdaQueryWrapper<IpaCollect> wrapper6 = new LambdaQueryWrapper<>();
        wrapper6.eq(IpaCollect::getAppletUserId,Integer.valueOf(s));
        List<IpaCollect> list6 = ipaCollectService.list(wrapper6);

        List<IpaEnter> carList6 = new ArrayList<>();
        for (int i = 0; i < list6.size(); i++) {
            Integer carId = list6.get(i).getIpaId();
            IpaEnter byId = ipaEnterService.getById(carId);
            carList6.add(byId);
        }

        IPage<IpaEnter> iPage = null;
            for (IpaEnter ipa : carList6) {
                Page<IpaEnter> recommendPage = new Page<>(page, limit);

                LambdaQueryWrapper<IpaEnter> wrapper1 = new LambdaQueryWrapper<>();

                Integer id = car.getId();

                if (id != null) {
                    wrapper1.orderByDesc(IpaEnter::getCreateTime).eq(IpaEnter::getId, ipa.getId());
                }

                iPage = ipaEnterService.page(recommendPage, wrapper1);

                for (IpaEnter record : iPage.getRecords()) {

                    LambdaQueryWrapper<IpaCollect> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(IpaCollect::getAppletUserId, Integer.valueOf(s)).eq(IpaCollect::getIpaId, record.getId());
                    IpaCollect one1 = ipaCollectService.getOne(wrapper2);

                    if (one1 != null) {
                        record.setCollect(1);
                    } else {
                        record.setCollect(0);
                    }
                }
            }
        return Result.ok(iPage);

    }

//    @ApiOperation(value = "程序的通讯录收藏详情")
//    @GetMapping("/selectIpa/{id}")
//    public Result<Ipa> selectIpa(@PathVariable Integer id){
//        Ipa byId = ipaService.getById(id);
//        byId.setCollect(byId.getCollect()+1);
//        return Result.ok(byId);
//    }

}

